package chapter6.eg9;

import java.io.Serializable;
import java.nio.ByteBuffer;

public class UserInfo implements Serializable {
  private static final long serialVersionUID = 1L;

  private String username;
  private int userId;

  public UserInfo buildUsername(String username) {
    this.username = username;
    return this;
  }

  public UserInfo buildUserId(int userId) {
    this.userId = userId;
    return this;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public int getUserId() {
    return userId;
  }

  public void setUserId(int userId) {
    this.userId = userId;
  }

  public byte[] codeC(ByteBuffer buffer) {
    buffer.clear();
    byte[] value = username.getBytes();
    buffer.putInt(value.length);
    buffer.put(value);
    buffer.putInt(userId);
    buffer.flip();
    value = null;
    byte[] result = new byte[buffer.remaining()];
    buffer.get(result);
    return result;
  }
}
